package com.sky.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 套餐分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO dto) {
        //开启分页查询
        PageHelper.startPage(dto.getPage(), dto.getPageSize());

        Page<Setmeal> page = setmealMapper.page(dto);
        return new PageResult(page.getTotal(), page.getResult());

    }

    /**
     * 新增套餐
     * @param dto
     */
    @Transactional
    public void addSetmeal(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        //将属性给setmeal对象
        BeanUtils.copyProperties(dto, setmeal);
        //插入套餐数据
        setmealMapper.addSetmeal(setmeal);
        //插入菜品数据
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        setmealDishMapper.addSetmealDish(setmealDishes);

    }
    /**
     * 根据id查询套餐
     * @param id
     */
    @Override
    @Transactional
    public SetmealDTO getById(Long id) {
        //查询套餐数据
        SetmealDTO  dto = setmealMapper.getById(id);
         //查询菜品数据
        List<SetmealDish> bySetmealId = setmealDishMapper.getBySetmealId(id);
        dto.setSetmealDishes(bySetmealId);
        return dto;

    }

    /**
     * 修改套餐
     * @param dto
     */
    @Override
    @Transactional
    public void update(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        //更新套餐
        setmealMapper.update(setmeal);
        //更新菜品
        //拿到后端与套餐绑定的菜品集合
        List<SetmealDish> bySetmealId = setmealDishMapper.getBySetmealId(setmeal.getId());
        //判断菜品集合是否为空
        if(!bySetmealId.isEmpty()){
            //如果绑定的菜品不为空，则批量删除菜品
            //取出id，其id应该都是一样的
            List<String> idList = bySetmealId.stream()
                    // 假设SetmealDish的id是Long类型，转为String；如果本身是String则直接获取
                    .map(dish -> dish.getSetmealId().toString())  // 关键：转为字符串
                    .collect(Collectors.toList());
                setmealDishMapper.deleteBySetmealId(idList);
        }
        //如果有数据进行先删除后添加,没有数据直接添加
        //拿到端传入的菜品
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        //注入id
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        setmealDishMapper.addSetmealDish(setmealDishes);
    }

    /**
     * 批量删除套餐，不仅要删除套餐表，还要删除与套餐绑定的菜品
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatch(String ids) {
        //转化为字符串数组
        String[] idArray = ids.split(",");
        List<String> list = Arrays.asList(idArray);
        //删除套餐表
        setmealMapper.deleteBatch(list);
        //删除菜品表
        setmealDishMapper.deleteBySetmealId(list);
    }

    /**
     * 起售、停售
     * @param status
     * @param id
     */
    //TODO
    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.update(setmeal);
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }


}
